package com.atguigu.gmall_sugar.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall_sugar.bean.ProductStats;
import com.atguigu.gmall_sugar.bean.ProvinceStats;
import com.atguigu.gmall_sugar.service.ProductStatsServiceImp;
import com.atguigu.gmall_sugar.service.ProvinceStatsServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: MyController
 * @Description:
 * @Author: kele
 * @Date: 2021/4/28 16:35
 **/
@RestController
@RequestMapping("/api/kele")
public class MyController {

    @Autowired
    ProductStatsServiceImp productStats;

    @Autowired
    ProvinceStatsServiceImp provinceStatsService;


    /**
     * 统计gmv的值，将结果传给sugar中的翻牌器进行显示
     */
    @RequestMapping("/gmv")
    public JSONObject getGMV(@RequestParam(value = "date",defaultValue = "0") int date){

        if(date == 0){
            //如果给date传的值为空，则使用当前天的日期
            date = Integer.parseInt( new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        //通过sql查询的方式获取  某天总的销售额
        BigDecimal gmv = productStats.getGMV(date);

        /**
         * 在sugar中要求的是一个json类型的字符串，需要拼接字符串
         * 且格式固定为：
         * {
         *   "status": 0,
         *   "msg": "",
         *   "data": 1201023.585469517
         * }
         */

        JSONObject obj = new JSONObject();

        obj.put("status",0);
        obj.put("msg","");
        obj.put("data",gmv);

        return obj;
    }



    @RequestMapping("/trademark")
    public String getProductStatsByTM(@RequestParam(value = "date",defaultValue = "0") int date,
                                      @RequestParam(value = "limit",defaultValue = "5") int limit){
        /**
         * {
         *   "status": 0,
         *   "msg": "",
         *   "data": {
         *     "categories": [
         *       "苹果",
         *       "三星",
         *       "华为"
         *     ],
         *     "series": [
         *       {
         *         "name": "手机品牌",
         *         "data": [
         *           9359,
         *           7390,
         *           6924
         *         ]
         *       }
         *     ]
         *   }
         * }
         */

        if(date == 0){
            //如果给date传的值为空，则使用当前天的日期
            date = Integer.parseInt( new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<ProductStats> productStatsByTrademarkList
                = productStats.getProductStatsByTM(date, limit);

        List<String> tradeMarkList = new ArrayList<>();
        List<BigDecimal> amountList = new ArrayList<>();
        for (ProductStats productStats : productStatsByTrademarkList) {
            tradeMarkList.add(productStats.getTm_name());
            amountList.add(productStats.getOrder_amount());
        }

        final HashMap<String, Object> result = new HashMap<>();

        result.put("status", 0);
        result.put("msg", "");

        final HashMap<String, Object> data = new HashMap<>();

        data.put("categories", tradeMarkList);

        final ArrayList<Map<String, Object>> series = new ArrayList();
        final HashMap<String, Object> one = new HashMap<>();
        one.put("name", "商品品牌");
        one.put("data", amountList);
        series.add(one);

        data.put("series", series);

        result.put("data", data);
        return JSON.toJSONString(result);

    }

    /**
     *
     * 获取某一天 spu的销售额
     * @param date
     * @param limit
     * @return
     */
    @RequestMapping("/spu")
    public JSONObject getProductStatsBySpu(@RequestParam(value = "date",defaultValue = "0") int date,
                                          @RequestParam(value = "limit",defaultValue = "5") int limit){

        if(date == 0){
            //如果给date传的值为空，则使用当前天的日期
            date = Integer.parseInt( new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
//通过sql查询的方式获取  某天总的销售额

        List<ProductStats> spu = productStats.getProductStatsBySpu(date, limit);

        /**
         * 在sugar中要求的是一个json类型的字符串，需要拼接字符串
         * 且格式固定为：
         *
         * {
         *   "status": 0,
         *   "msg": "",
         *   "data": {
         *     "columns": [
         *       {
         *         "name": "分组织",
         *         "id": "subsys"
         *       },
         *       {
         *         "name": "部门",
         *         "id": "module"
         *       }
         *     ],
         *     "rows": [
         *       {
         *         "subsys": "北京总部",
         *         "module": "市场部",
         *       },
         *       {
         *         "subsys": "北京总部",
         *         "module": "市场部",
         *       }
         *     ]
         *   }
         * }
         *
         */

        JSONObject obj = new JSONObject();

        obj.put("status",0);
        obj.put("msg","");

        JSONObject data = new JSONObject();

        JSONArray columns = new JSONArray();

            JSONObject c1 = new JSONObject();
            c1.put("name", "商品名次");
            c1.put("id", "spu_name");
                columns.add(c1);
            JSONObject c2 = new JSONObject();
            c2.put("name", "交易额");
            c2.put("id", "order_amount");
                columns.add(c2);
            JSONObject c3 = new JSONObject();
            c3.put("name", "订单数");
            c3.put("id", "order_ct");
                columns.add(c3);

        JSONArray rows = new JSONArray();

        for (ProductStats productStats : spu) {
            final HashMap<String, Object> row = new HashMap<>();
            row.put("spu_name", productStats.getSpu_name());
            row.put("order_amount", productStats.getOrder_amount());
            row.put("order_ct", productStats.getOrder_ct());
            rows.add(row);
        }

        data.put("columns",columns);
        data.put("rows",rows);

        obj.put("data",data);

        return obj;
    }

    @RequestMapping("/c3")
    public JSONObject getProductStatsByC3(@RequestParam(value = "date",defaultValue = "0") int date,
                                           @RequestParam(value = "limit",defaultValue = "5") int limit){

        if(date == 0){
            //如果给date传的值为空，则使用当前天的日期
            date = Integer.parseInt( new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
//通过sql查询的方式获取  某天总的销售额

        List<ProductStats> category = productStats.getProductStatsByCategory(date, limit);

        /**
         * 在sugar中要求的是一个json类型的字符串，需要拼接字符串
         * 且格式固定为：
         *
         * {
         *   "status": 0,
         *   "msg": "",
         *   "data": [
         *     {
         *       "name": "PC",
         *       "value": 97,
         *       "url": "http://www.baidu.com"
         *     },
         *     {
         *       "name": "iOS",
         *       "value": 50,
         *       "url": "http://www.baidu.com"
         *     }
         *   ]
         * }
         *
         */

        JSONObject obj = new JSONObject();

        obj.put("status",0);
        obj.put("msg","");

        JSONArray data = new JSONArray();

        for (ProductStats stats : category) {

            JSONObject datajson = new JSONObject();
            datajson.put("name",stats.getCategory3_name());
            datajson.put("value",stats.getOrder_amount());

            data.add(datajson);
        }

        obj.put("data",data);

        return obj;
    }

    @RequestMapping("/province")
    public JSONObject getProductStatsByC3(@RequestParam(value = "date",defaultValue = "0") int date){

        if(date == 0){
            //如果给date传的值为空，则使用当前天的日期
            date = Integer.parseInt( new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
//通过sql查询的方式获取  某天总的销售额

        List<ProvinceStats> pss = provinceStatsService.getProvinceStatsByName(date);

        /**
         * 在sugar中要求的是一个json类型的字符串，需要拼接字符串
         * 且格式固定为：
         *
         * {
         *   "status": 0,
         *   "msg": "",
         *   "data": [
         *     {
         *       "name": "PC",
         *       "value": 97,
         *       "url": "http://www.baidu.com"
         *     },
         *     {
         *       "name": "iOS",
         *       "value": 50,
         *       "url": "http://www.baidu.com"
         *     }
         *   ]
         * }
         *
         */

        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();
        for (ProvinceStats ps : pss) {
            JSONObject p = new JSONObject();
            p.put("name", ps.getProvince_name());
            p.put("value", ps.getOrder_amount());


            p.put("tooltipValues", new Object[]{ps.getOrder_count()});

            mapData.add(p);
        }
        data.put("mapData", mapData);

        data.put("valueName", "销售额");

        data.put("tooltipNames", new Object[]{"订单数"});

        result.put("data", data);

        return result;
    }
}
